<?php

use Biz\service\course\CourseOrder;
use Biz\service\trade\Pay;
use Biz\service\trade\PaySuccess;
use Doraemon\tools\ActionLog;
use Doraemon\tools\Log;

class ResultCallBackAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;

    public function main()
    {
        $orderNo = $this->getRequest()->getPost('order_no');
        $payWay = $this->getRequest()->getPost('pay_way');
        $payResult = $this->getRequest()->getPost('pay_result');
        $orderType = $this->getRequest()->getPost('order_type', Pay::_PAY_ORDER_TYPE_BIZ);
        $receiptData = $this->getRequest()->getPost('receipt_data', null);
        $restore = $this->getRequest()->getPost('restore', 1);
        Log::debug('支付回调调用记录', [
            'order_no'     => $orderNo,
            'pay_way'      => $payWay,
            'pay_result'   => $payResult,
            'order_type'   => $orderType,
            'receipt_data' => $receiptData,
            'restore'      => $restore
        ]);
        switch ($orderType) {
            case Pay::_PAY_ORDER_TYPE_BIZ:
                $response = PaySuccess::main($this->uid, $orderNo);
                break;
            case Pay::_PAY_ORDER_TYPE_COURSE:
                switch ($payWay) {
                    case Pay::_PAY_WAYS_ALIPAY:
                    case Pay::_PAY_WAYS_WECHAT:
                    case Pay::_PAY_WAYS_WECHAT_BIZ:
                        $response = PaySuccess::courseOrder($this->uid, $orderNo, $payWay);
                        break;
                    case Pay::_PAY_WAYS_IAP:
                        $response = PaySuccess::courseIAPOrder($this->uid, $orderNo, $receiptData);
                        break;
                    case Pay::_PAY_WAYS_TILES:
                        $response = PaySuccess::courseTilesOrder($this->uid, $orderNo);
                        break;
                    default:
                        throw new \Exception('参数错误');
                        break;
                }
                $orderInfo = CourseOrder::getOrderInfo($orderNo);
                if ($orderInfo) {
                    ActionLog::log($this->uid, [
                        'order_no'  => $orderNo,
                        'course_id' => $orderInfo['course_id'],
                        'pay_way'   => $orderInfo['pay_way'],
                        'pay_time'  => $orderInfo['pay_time'],
                        'pay_no'    => $orderInfo['pay_no']
                    ], ActionLog::BIZ_COURSE_PAY_SUCCESS);
                }
                break;
            case Pay::_PAY_ORDER_TYPE_PACKAGE:
                switch ($payWay) {
                    case Pay::_PAY_WAYS_ALIPAY:
                    case Pay::_PAY_WAYS_WECHAT:
                    case Pay::_PAY_WAYS_WECHAT_BIZ:
                        $response = PaySuccess::packageOrder($this->uid, $orderNo, $payWay);
                        break;
                    default:
                        throw new \Exception('参数错误');
                        break;
                }
                break;
            case Pay::_PAY_ORDER_TYPE_TILES:
                $response = PaySuccess::TilesIAPOrder($this->uid, $receiptData);
                break;
            default:
                throw new \Exception('参数错误');
                break;
        }
        $this->setSuccess($response);
    }
}